package org.jdsm.flow.gxt.users.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.info.Info;
import org.jdsm.flow.gxt.users.client.widgets.user.UserGroupGrid;
import org.jdsm.flow.gxt.users.client.widgets.user.UserRoleGrid;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 10/3/12
 * Time: 3:23 PM
 */
public class DistributionOfPermission implements IsWidget {
    interface DistributionOfPermissionUiBinder extends UiBinder<Widget, DistributionOfPermission> {
    }

    private static DistributionOfPermissionUiBinder ourUiBinder = GWT.create(DistributionOfPermissionUiBinder.class);

    public DistributionOfPermission() {

    }

    @Override
    public Widget asWidget() {
        Widget rootElement = ourUiBinder.createAndBindUi(this);
        return rootElement;
    }

    @UiField
    protected UserRoleGrid userRoleGrid;
    @UiField protected UserGroupGrid userGroupGrid;
    @UiField
    protected Window window;

    @UiHandler({"saveBtn"})
    public void saveOrUpdateUserRoles(SelectEvent event){
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
        userRoleGrid.saveData();
        userRoleGrid.saveAction(null);
        Info.display("Hello", "How are you?");
    }

    @UiHandler({"showWindowBtn"})
    public void showWindow(SelectEvent event){
        window.show();
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
        UserAutoBeanFactory.User user = factory.create(UserAutoBeanFactory.User.class).as();
//        user.setId("121312");
        userGroupGrid.loadDataByUser(user);
        userRoleGrid.loadDataByUser(user);
    }


}